package com.gtq.uhome.service.serviceImpl;

import com.gtq.uhome.mapper.OrderMapper;
import com.gtq.uhome.mapper.UserLoginMapper;
import com.gtq.uhome.service.UserLoginService;
import com.gtq.uhome.vo.UserOrderCheckVo;
import com.gtq.uhome.vo.UserVo;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 前端用户登录模块接口
 */
@Service
public class UserLoginServiceImpl implements UserLoginService {

    @Autowired
    OrderMapper orderMapper;

    @Resource
    HttpClient client=null;
    @Resource
    PostMethod method=null;
    @Value("${sms.account}")
    String account;
    @Value("${sms.password}")
    String password;
    @Resource
    private UserLoginMapper userLoginMapper;
    /**
     * 用户通过手机号和验证码登录
     * @param phoneNumber 用户手机号
     * @param verificationCode 用户验证码
     * @return 返回true 登录成功过 false返回失败
     */
    @Override
    public UserVo userLogin(String phoneNumber, Integer verificationCode) {
        //调用方法
        UserVo userVo = userLoginMapper.userLogin(phoneNumber, verificationCode);
        //返回用户ID
        return userVo;
    }

    /**
     * 前端用户注册界面信息展示
     *
     * @param siteName 站点名称 为null 展示总站点客服电话
     * @return 返回用户协议 和 站点客服电话号码
     */
    @Override
    public Map<String, String> registerInfo(String siteName) {
        //查询站点电话
        String sitePhoneNumber;
        if(siteName!=null){
            sitePhoneNumber = userLoginMapper.selectSitePhoneNumber(siteName);
        }else {
            //设置总店
            siteName="华阳店";
            sitePhoneNumber = userLoginMapper.selectSitePhoneNumber(siteName);
        }
        //查询用户协议
        String userAgreement = userLoginMapper.selectagreement();
        //生成一个Map容器
        Map<String,String> registerInfo=new HashMap<>();
        //填入信息
        registerInfo.put("sitePhoneNumber",sitePhoneNumber);
        registerInfo.put("userAgreement",userAgreement);
        return registerInfo;
    }

    @Override
    public Integer sendVerificationCode(String phoneNumber) {
        client.getParams().setContentCharset("GBK");
        method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=GBK");

        int mobile_code = (int) ((Math.random() * 9 + 1) * 100000);
        System.out.println("验证码："+mobile_code);
        String content = new String("[小U到家]您的验证码是：" + mobile_code + "。请不要把验证码泄露给其他人。");

        //提交短信
        NameValuePair[] data = {

                //  name 和 password 需要自行修改。
                new NameValuePair("account", account), //查看用户名                是登录用户中心->验证码短信->产品总览->APIID
                new NameValuePair("password", password),  //查看密码请登录用户中心->验证码短信->产品总览->APIKEY
                new NameValuePair("mobile", phoneNumber),
                new NameValuePair("content", content),
        };
        method.setRequestBody(data);

        try {
            client.executeMethod(method);

            String SubmitResult = method.getResponseBodyAsString();

            System.out.println(SubmitResult);

            Document doc = DocumentHelper.parseText(SubmitResult);
            Element root = doc.getRootElement();

            String code = root.elementText("code");
            String msg = root.elementText("msg");
            String smsid = root.elementText("smsid");

            System.out.println(code);
            System.out.println(msg);
            System.out.println(smsid);

            if ("2".equals(code)) {
                System.out.println("短信提交成功");

                return mobile_code;  //验证码
            } else return 0;

        } catch (Exception e) {
            e.printStackTrace();
            return 0;

        }
    }
    /**
     * 用户注册方法
     * @param userName 用户姓名
     * @param userPhoneNumber 用户电话号码
     * @return 返回影响的行数
     */
    @Override
    public Boolean register(String userName, String userPhoneNumber) {
        //先查询用户是否注册过
        Integer integer = userLoginMapper.selectUserByPhoneNumber(userPhoneNumber);
        //如果注册过直接登录
        if (integer!=null){
            return false;
        }
        //反之注册
        Integer register = userLoginMapper.register(userName, userPhoneNumber);
        if (register>0){
            return true;
        }
        return false;
    }

    @Override
    public List<UserOrderCheckVo> findUserCheckOderList(Integer userId) {
        return orderMapper.findUserCheckOderList(userId);
    }

    /**
     * 查询用户消息
     * @param userId 用户ID
     * @return 返回消息集合
     */
    @Override
    public List<String> userMsg(Integer userId) {
        List<String> strings = userLoginMapper.selectUserMsg(userId);
        return strings;
    }
}
